package com.yidushishizijie;

/*
* 题目：三个售票员    卖出      30张票
*
* 多线程编程的企业级套路+模板
*
* 1.在高内聚低耦合的前提下，线程          操作（对外暴露的调用方法）     资源类
* */

public class SaleTicket {

    public static void main(String[] args) {

        Ticket ticket = new Ticket();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 40 ; i++) {
                    ticket.saleTicket();
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 40 ; i++) {
                    ticket.saleTicket();
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 40 ; i++) {
                    ticket.saleTicket();
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"C").start();
    }
}


class Ticket{//资源类

    private int num = 30;//票数有30张

    public synchronized void saleTicket(){
        if(num > 0){
            System.out.println(Thread.currentThread().getName()+"\t 卖出第："+(num--)+"\t 还剩下："+num);
        }
    }
}